package ar.com.larctest.forms;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import ar.com.larctest.R;
import ar.com.larctest.adapters.ListViewRaceHorseAdapter;
import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.Horses;
import ar.com.larctest.providers.HorsesProvider;

public class HorsesDataActivity extends Activity {
    private static final String TAG = "HorsesDataActivity";

    //Selected Horse
    Horses selectedHorse;

    private ListViewRaceHorseAdapter raceHorseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horses_data);
        Bundle bundle = getIntent().getExtras();

        SQLLARCTESTHelper sqlHelper = SQLLARCTESTHelper.getHelper(HorsesDataActivity.this);
        sqlHelper.dbOpen();

        try {

            Integer idHorse =  bundle.getInt("selectedHorse");

            HorsesProvider horsesProvider = new HorsesProvider(HorsesDataActivity.this);
            selectedHorse = horsesProvider.getHorse(idHorse);

            TextView horseName = (TextView) findViewById(R.id.name);
            TextView yearsOld = (TextView) findViewById(R.id.horseYearsOld);
            TextView hair = (TextView) findViewById(R.id.hair);
            TextView temperament = (TextView) findViewById(R.id.temperament);

            ListView listViewRaceHorseData = (ListView) findViewById(R.id.lvRaceHorse);

            raceHorseAdapter = new ListViewRaceHorseAdapter(HorsesDataActivity.this, R.id.lvRaceHorse, selectedHorse.getRacings());
            View header = getLayoutInflater().inflate(R.layout.listview_header_race_horse, null);
            listViewRaceHorseData.addHeaderView(header);

            listViewRaceHorseData.setAdapter(raceHorseAdapter);

            horseName.setText(selectedHorse.getName());
            yearsOld.setText(selectedHorse.getYearsOld().toString());
            hair.setText(selectedHorse.getHair());
            temperament.setText(selectedHorse.getTemperament());

        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        finally {
            sqlHelper.dbClose();
        }
    }
}
